package com.scancode.ocr;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;

import androidx.lifecycle.LifecycleOwner;

/**
 * 项目名称：com.scancode.ocr
 * 创建人:LuoFG
 * 创建时间:2021/12/10  14:41
 * 描述:
 * 作用: 身份证识别客户端
 */
public class OcrIDClient {

    private static class InstanceHolder {
        private static OcrIDClient instance = new OcrIDClient();
    }

    private static OcrIDClient getInstance() {
        return OcrIDClient.InstanceHolder.instance;
    }

    public static OcrIDClient getClient() {
        return getInstance();
    }

    /***配置资源-Start***/
    //是否显示左边返回键文字
    private boolean isShowBackText = false;
    //补光功能是否开放
    private boolean isOpenLight = true;
    //默认Bar文字白色
    private int barTextColor = Color.WHITE;
    //默认Bar背景白色
    private int barBgColor = Color.BLUE;
    //默认Bar返回键
    private int imgBack = R.mipmap.ic_arrow_back_ocr;

    //是否自动缩放
    private boolean isAutoZoom = false;
    //扫描框边角线默认颜色（折角）  白色
    private int scanCornerColor = Color.WHITE;
    //扫描线默认颜色  白色
    private int scanLineColor = Color.WHITE;
    //Bar返回提示
    private String barBackText="返回";
    //Bar标题提示
    private String barTitleText="扫一扫";
    /***配置资源-End***/
    //传入具体包名类  作为区分
    private String ZxingTag="";
    //标题栏颜色是否沉浸到状态栏
    private boolean isToOsBar = false;

    //设置成为默认属性
    public OcrIDClient setDefault() {
        //是否显示左边返回键文字
        isShowBackText = false;
        //补光功能是否开放
        isOpenLight = true;
        //是否自动缩放
        isAutoZoom = true;
        //默认Bar文字白色
        barTextColor = Color.WHITE;
        //默认Bar背景白色
        barBgColor = Color.BLUE;
        //默认Bar返回键
        imgBack = R.mipmap.ic_arrow_back_ocr;

        //是否是条形码框 true为条形码
        isAutoZoom = false;
        //扫描框边角线默认颜色（折角）  白色
        scanCornerColor = Color.WHITE;
        //扫描线默认颜色  白色
        scanLineColor = Color.WHITE;
        barBackText="返回";
        barTitleText="身份证识别";
        isToOsBar=false;

        return this;
    }

    public interface ScanResult{
        void back(OcrResultBean result);
    }

    public OcrIDClient setResultListener(Activity activity, OcrIDClient.ScanResult scanResult){
        try {
            OcrIDActivity.ocrResult.observe((LifecycleOwner) activity, s -> {
                //Log.e("收到的识别回调",s.getTag()+s.getResult());
                scanResult.back(s);
                OcrIDActivity.ocrResult.removeObservers((LifecycleOwner) activity);
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return this;
    }

    /**
     * @param activity 启动Ocr识别
     */
    public void startOcr(Activity activity){
        activity.startActivity(new Intent(activity, OcrIDActivity.class));
    }

    public OcrIDClient setDebug(boolean debug){
        return this;
    }

    public OcrIDClient setToOsBar(boolean toOsBar){
        isToOsBar=toOsBar;
        return this;
    }

    public boolean isToOsBar() {
        return isToOsBar;
    }

    public String getZxingTag() {
        return ZxingTag;
    }

    public OcrIDClient setZxingTag(String zxingTag) {
        ZxingTag = zxingTag;
        return this;
    }

    public boolean isAutoZoom() {
        return isAutoZoom;
    }

    public OcrIDClient setAutoZoom(boolean autoZoom) {
        isAutoZoom = autoZoom;
        return this;
    }


    public int getScanCornerColor() {
        return scanCornerColor;
    }

    /**
     * @param scanCornerColor  此方法暂时不生效
     * @return
     */
    public OcrIDClient setScanCornerColor(int scanCornerColor) {
        this.scanCornerColor = scanCornerColor;
        return this;
    }

    public String getBarTitleText() {
        return barTitleText;
    }

    public OcrIDClient setBarTitleText(String barTitleText) {
        this.barTitleText = barTitleText;
        return this;
    }

    public String getBarBackText() {
        return barBackText;
    }

    public OcrIDClient setBarBackText(String barBackText) {
        this.barBackText = barBackText;
        return this;
    }

    public boolean isShowBackText() {
        return isShowBackText;
    }

    public OcrIDClient setShowBackText(boolean showBackText) {
        isShowBackText = showBackText;
        return this;
    }

    public boolean isOpenLight() {
        return isOpenLight;
    }

    public OcrIDClient setOpenLight(boolean OpenLight) {
        isOpenLight = OpenLight;
        return this;
    }

    public int getBarTextColor() {
        return barTextColor;
    }

    public OcrIDClient setBarTextColor(int barTextColor) {
        this.barTextColor = barTextColor;
        return this;
    }

    public int getBarBgColor() {
        return barBgColor;
    }

    public OcrIDClient setBarBgColor(int barBgColor) {
        this.barBgColor = barBgColor;
        return this;
    }

    public int getImgBack() {
        return imgBack;
    }

    public OcrIDClient setImgBack(int imgBack) {
        this.imgBack = imgBack;
        return this;
    }


    public int getScanLineColor() {
        return scanLineColor;
    }

    public OcrIDClient setScanLineColor(int scanLineColor) {
        this.scanLineColor = scanLineColor;
        return this;
    }
}
